<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\RoomSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Service\AdminUserService;
use App\Application\Admin\Service\RoomlabelService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Store;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Database\Model\Relations\Relation;
use Hyperf\DbConnection\Model\Model;
use PhpParser\Node\Stmt\Switch_;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/room")]
class RoomController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        $room_name = $this->request->input('room_name', '');
        $store_id = $this->request->input('store_id', '');
        if ($room_name != '') {
            $where[] = ['room_name', 'like', '%' . $room_name . '%'];
        }
        if ($store_id != '') {
            $where[] = ['store_id', '=', $store_id];
        }
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            $where[] = ['admin_id', '=', $admin_user['admin_user_id']];
        }
        $lists = Room::where($where)
            ->orderBy('sort', 'DESC')
            ->orderBy('store_id', 'DESC')
            ->paginate()->toArray();
        $admin=$this->getadminid();
        if ($this->getadminid() !== 1) {
            $store_list = Store::where('admin_id', '=', $this->getadminid())
                ->orderBy('created_at', 'DESC')
                ->pluck('store_name', 'store_id');
        } else {
            $store_list = Store::where([])
                ->orderBy('created_at', 'DESC')
                ->pluck('store_name', 'store_id');
        }
        foreach ($lists['data'] as $k => $v) {
            //店铺名称
            $lists['data'][$k]['store_name'] = $store_list[$v['store_id']];

        }
        return compact('lists', 'store_list','admin');
    }

    #[Api]
    #[GetMapping("index/getlists/{store_id}")]
    function getlists(int $store_id)
    {
        $where = [];
        if ($store_id != '') {
            $where[] = ['store_id', '=', $store_id];
        }
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            $where[] = ['admin_id', '=', $admin_user['admin_user_id']];
        }
        $roomOptions = Room::where($where)
            ->orderBy('sort', 'DESC')
            ->orderBy('store_id', 'DESC')
            ->select(['name', 'room_id'])->get()->toArray();

        return compact('roomOptions');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new RoomSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $room_id = $request_param->getRoomId();
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        $res = Room::createStore($room_id, [
            'name' => $request_param->getName()
            , 'image_url' => $request_param->getImageUrl()
            , 'package_id' => $request_param->getPackageId()
            , 'label' => $request_param->getLabel()
            , 'door_id' => $request_param->getDoorId()
            , 'door_type' => $request_param->getDoorType()
            , 'store_id' => $request_param->getStoreId()
            , 'status' => $request_param->getStatus()
            , 'admin_id' => $admin_user['admin_user_id']
        ]);
        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $room_id = (int)$this->request->input('room_id', 0);

        return ['title' => $room_id > 0 ? '编辑包厢' : '新增包厢'];
    }


    #[Api]
    #[GetMapping("edit/{room_id}")]
    function editInfo(int $room_id)
    {
        $labelOptions = RoomlabelService::getInstance()
            ->getAllroomlabel();
        $storeOptions = Store::where('admin_id', $this->getadminid())->select('*')->get()
            ->toArray();
        $packageOptions = Package::where('deleted_at', null)
            ->orderBy('created_at')
            ->select([
                'package_id', 'name', 'price', 'type'
            ])
            ->get()
            ->toArray();
        $room = Room::where('room_id', $room_id)->select('*')
            ->first() ?: [];
        return compact('labelOptions', 'room', 'packageOptions', 'storeOptions');
    }

    #[Api]
    #[RequestMapping("open/{door_id}/{door_type}", ["POST", "PUT"])]
    function open(string $door_type, int $door_id)
    {
        $res = $this->adminopen($door_type, $door_id, 90);
        return $res ? $this->returnSuccessJson(compact('res'), '通电成功') : $this->returnErrorJson();
    }

    #[Api]
    #[RequestMapping("close/{door_id}/{door_type}", ["POST", "PUT"])]
    function close(string $door_type, int $door_id)
    {

        $res = $this->adminopen($door_type, $door_id, 0);
        return $res ? $this->returnSuccessJson(compact('res'), '通电成功') : $this->returnErrorJson();
    }

}
